package test.framework;

import fox.framework.rpc.client.RpcRegister;
import fox.framework.rpc.client.invocation.ConsumeInvoker;
import fox.framework.rpc.client.invocation.ConsumeInvokerChain;
import fox.framework.rpc.client.register.ZookeeperRegister;
import fox.framework.rpc.context.DefaultRpcApplicationContext;
import fox.framework.rpc.context.RpcApplicationConfigureAdapter;
import fox.framework.rpc.context.RpcApplicationContext;
import fox.framework.rpc.context.RpcDefinitionRegister;
import fox.framework.rpc.context.definition.AbstractRpcDefinition;
import fox.framework.rpc.context.definition.RpcDefinitionBuilder;
import fox.framework.rpc.context.generator.name.RpcNameGenerator;
import fox.framework.rpc.context.processor.RpcDefinitionRegisterProcessor;
import test.framework.service.ServiceA;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;

/**
 * @author cuichao
 * @Description: Client
 * @Date: create in 2021/2/4 21:02
 */
public class Client {


    public static void main(String[] args) {

        ClientConfig config = new ClientConfig();
        DefaultRpcApplicationContext application = new DefaultRpcApplicationContext(config);

        try {
            application.run();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("客户端");
        ServiceA object = application.getObject(ServiceA.class);
        String a = object.a("测试1");
        System.out.println("第一次调用: "+a);

      /*  String b = object.a("测试2");
        System.out.println("第二次调用: "+b);*/
    }


    public static class ClientConfig extends RpcApplicationConfigureAdapter {

        @Override
        public String getApplicationName() {
            return "testClientApp";
        }

        @Override
        public String getGlobalProtocol() {
            return "fox";
        }

        @Override
        public int getPort() {
            return 6666;
        }

        @Override
        public String getBind() {
            return "127.0.0.1";
        }

        @Override
        public RpcRegister getRegister() {
            return new ZookeeperRegister();
        }

        @Override
        protected RpcDefinitionRegisterProcessor addDefinitionRegisterProcessor() {
            return new RpcDefinitionRegisterProcessor() {
                @Override
                public void registerRpcDefinition(RpcNameGenerator generator, RpcDefinitionRegister register) {
                    RpcDefinitionBuilder builder = RpcDefinitionBuilder.genericRpcDefinition();
                    builder.setMetaspace("ServiceA");
                    builder.setServiceInterface(ServiceA.class);
                    builder.setProvider(false);
                    AbstractRpcDefinition definition = builder.getDefinition();
                    String name = generator.generateRpcName(definition, register);
                    register.register(name,definition);
                }

                @Override
                public void process(RpcApplicationContext context) {

                }
            };
        }


        @Override
        protected List<ConsumeInvoker> addConsumeInvokers() {
            return Collections.singletonList(new ConsumeInvoker() {
                @Override
                public Object doInvoke(ConsumeInvokerChain chain, Method method, Object[] args) throws Throwable {
                    System.out.println("方法名:"+method.getName());
                    return chain.doChain(chain,method,args);
                }
            });
        }
    }


}
